﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dapper;
using LibMS.Models;

namespace LibMS.Controllers
{
    [Authorize(Roles = "System Admin, Lib Admin")]
    public class ApiLibRackController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetAll()
        {
            int? total;
            string message;
            var data = _unitOfWork.LibRack.QuerySp(out total, out message, StringConstant.TABLE_LIBRACK + StringConstant.SP_BASE_GET);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var sqlResult = data as List<LibRack> ?? null;
            var result = new
            {
                success = true,
                message = string.Empty,
                total = total,
                data = sqlResult
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetById(int rackId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@RackId", rackId);
            var data = _unitOfWork.LibRack.GetById(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = data
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage UpdateData(LibRack entity)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@RackId", entity.RackId);
            parameters.Add("@RackName", entity.RackName);
            var data = _unitOfWork.LibRack.Update(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = entity
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage CreateData(LibRack entity)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@RackName", entity.RackName);
            var data = _unitOfWork.LibRack.Create(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = entity
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage DeleteData(int rackId)
        {
            string message1;
            var parameters1 = new DynamicParameters();
            parameters1.Add("@RackId", rackId);
            var data1 = _unitOfWork.LibRack.Delete(out message1, parameters1);
            if (data1 == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message1
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = rackId
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
